package org.calzz.rpc.envelope;

/**
 * @author Marek Brodziak <marek.brodziak@gmail.com>
 * @author Piotr Pejas <piotr.pejas@gmail.com>
 * @since Mar 6, 2008
 */
public class JsonInvocationException extends Exception {
	private static final long serialVersionUID = -9212418982769531446L;

	private final JsonInvocationError error;
	
	public JsonInvocationException(JsonInvocationError error, String message) {
		this(error, message, null);
	}

	public JsonInvocationException(JsonInvocationError error, String message, Throwable throwable) {
		super(message, throwable);
		this.error = error;
		JsonExceptionEnvelope envelope = new JsonExceptionEnvelope();
		envelope.setClassName(getStackTrace()[0].getClassName());
		envelope.setMethodName(getStackTrace()[0].getMethodName());
		envelope.setLineNumber(getStackTrace()[0].getLineNumber());
		envelope.setMessage(message);
		if (throwable != null) {
			envelope.setException(throwable.getClass().getName());
			envelope.setExceptionMessage(throwable.getMessage());
		}
		this.error.setError(envelope);
	}

	public JsonInvocationException(JsonInvocationError error) {
		this(error, null, null);
	}

	public JsonInvocationError getError() {
		return error;
	}
}
